<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="攻略名称" prop="pointStrategyName">
        <el-input
          v-model="queryParams.pointStrategyName"
          placeholder="请输入攻略名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="点位" prop="pointId">
        <el-cascader :props="props" v-model="queryParams.pointId"></el-cascader>
      </el-form-item>
      <el-form-item label="英雄" prop="heroId">
        <el-select v-model="queryParams.heroId" placeholder="请选择英雄">
          <el-option
            v-for="item in heroList"
            :key="item.heroId"
            :label="item.heroName"
            :value="item.heroId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tile:strategy:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tile:strategy:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tile:strategy:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tile:strategy:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="strategyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="攻略id" align="center" prop="pointStrategyId" />
      <el-table-column
        label="攻略名称"
        align="center"
        prop="pointStrategyName"
      />
      <el-table-column label="点位" align="center" prop="pointName" />
      <el-table-column label="英雄" align="center" prop="heroName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tile:strategy:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tile:strategy:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改点位攻略对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="攻略名称" prop="pointStrategyName">
          <el-input
            v-model="form.pointStrategyName"
            placeholder="请输入攻略名称"
          />
        </el-form-item>
        <el-form-item label="点位" prop="pointId">
          <!-- <el-input v-model="form.pointId" placeholder="请输入点位id" /> -->
          <el-cascader :props="props" v-model="form.pointId"></el-cascader>
        </el-form-item>
        <el-form-item label="英雄" prop="heroId">
          <el-select v-model="form.heroId" placeholder="请选择英雄">
            <el-option
              v-for="item in heroList"
              :key="item.heroId"
              :label="item.heroName"
              :value="item.heroId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStrategy,
  getStrategy,
  delStrategy,
  addStrategy,
  updateStrategy,
} from "@/api/tile/strategy";
import { listMap } from "@/api/tile/map";
import { listPoint } from "@/api/tile/point";
import { listHero } from "@/api/tile/hero";
export default {
  name: "Strategy",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 点位攻略表格数据
      strategyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pointStrategyName: null,
        pointId: [],
        heroId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pointStrategyName: [
          { required: true, message: "攻略名称不能为空", trigger: "blur" },
        ],
        pointId: [
          { required: true, message: "点位id不能为空", trigger: "blur" },
        ],
        heroId: [
          { required: true, message: "英雄id不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" },
        ],
      },
      heroList: [],
      pointList: [],
      props: {
        lazy: true,
        // emitPath:false,
        // checkStrictly: true,
        lazyLoad(node, resolve) {
          const { level, value } = node;
          if (level == 0) {
            listMap({ pageNum: 1, pageSize: 50 }).then((response) => {
              response.rows.map((item) => {
                item.value = item.mapId;
                item.label = item.mapName;
              });
              resolve(response.rows);
            });
          } else {
            listPoint({ mapId: value, pageNum: 1, pageSize: 500 }).then(
              (response) => {
                response.rows.map((item) => {
                  item.value = item.pointId;
                  item.label = item.pointName;
                  item.leaf = true;
                });
                resolve(response.rows);
              }
            );
          }
        },
      },
    };
  },
  async created() {
    await this.getHeroList();
    await this.getpointList();
    await this.getList();
  },
  methods: {
    /** 查询点位攻略列表 */
    getList() {
      this.loading = true;
      const pointId =
        this.queryParams.pointId.length == 2 ? this.queryParams.pointId[1] : "";
      const queryParams = { ...this.queryParams, pointId };
      listStrategy({ ...this.queryParams, pointId }).then((response) => {
        response.rows.map((item) => {
          item.heroName = this.heroList.find(
            (heroItem) => heroItem.heroId === item.heroId
          ).heroName
          item.pointName = this.pointList.find(
            (pointItem) => pointItem.pointId === item.pointId
          ).pointName
        });
        console.log('response.rows',response.rows);
        this.strategyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    async getHeroList() {
      const response = await listHero({ pageNum: 1, pageSize: 50 })
        this.heroList = response.rows;
    },
    async getpointList() {
      const response = await listPoint({ pageNum: 1, pageSize: 500 })
        this.pointList = response.rows;
    },
    cascaderChange(value) {
      console.log("value", value);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pointStrategyId: null,
        pointStrategyName: null,
        pointId: null,
        heroId: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.pointStrategyId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加点位攻略";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pointStrategyId = row.pointStrategyId || this.ids;
      getStrategy(pointStrategyId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改点位攻略";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.pointId = this.form.pointId[1];
          if (this.form.pointStrategyId != null) {
            updateStrategy(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStrategy(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pointStrategyIds = row.pointStrategyId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除点位攻略编号为"' + pointStrategyIds + '"的数据项？'
        )
        .then(function () {
          return delStrategy(pointStrategyIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "tile/strategy/export",
        {
          ...this.queryParams,
        },
        `strategy_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
